//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package cn.hutool.core.img;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

public class GraphicsUtil {
    public GraphicsUtil() {
    }

    public static Graphics2D createGraphics(BufferedImage image, Color color) {
        Graphics2D g = image.createGraphics();
        if (null != color) {
            g.setColor(color);
            g.fillRect(0, 0, image.getWidth(), image.getHeight());
        }

        return g;
    }

    public static int getCenterY(Graphics g, int backgroundHeight) {
        FontMetrics metrics = null;

        try {
            metrics = g.getFontMetrics();
        } catch (Exception var4) {
        }

        int y;
        if (null != metrics) {
            y = (backgroundHeight - metrics.getHeight()) / 2 + metrics.getAscent();
        } else {
            y = backgroundHeight / 3;
        }

        return y;
    }

    public static Graphics drawStringColourful(Graphics g, String str, Font font, int width, int height) {
        return drawString(g, str, font, Color.black, width, height);
    }

    public static Graphics drawString(Graphics g, String str, Font font, Color color, int width, int height) {
        if (g instanceof Graphics2D) {
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        }

        g.setFont(font);
        int midY = getCenterY(g, height);
        if (null != color) {
            g.setColor(color);
        }

        int len = str.length();
        int charWidth = width / len;

        for(int i = 0; i < len; ++i) {
            if (null == color) {
                g.setColor(ImgUtil.randomColor());
            }

            g.drawString(String.valueOf(str.charAt(i)), i * charWidth, midY);
        }

        return g;
    }
}
